import { BadGatewayException, Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';
import { QiyeModel } from 'src/mongodb/models/qiyeModel/qiyeModel';
import { addCompanyDto, deleteCompanyDto, getCompanyDto, searchCompanyDto, updateCompanyDto } from './companyDto/companyDto';

@Controller('web')
@UseGuards(AuthGuard("jwtweb"))
export class CompanyController {

    constructor(@InjectModel(QiyeModel) private readonly QiyeModel: ModelType<QiyeModel>) { }


    /**
     * @Description: 添加公司
     * @param companyName {String} 企业名称
     * @param score {String}  企业评分
     * @return: Promise
     */
    @Post('addCompany')
    async addCompany(@Body() body: addCompanyDto) {
        try {
            await this.QiyeModel.create({ companyName: body.companyName, score: body.score, createTime: Math.floor(Date.now() / 1000) })
            return { code: 200, message: '添加成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }

    }


    /**
     * @Description: 修改公司状态
     * @param id {String} 公司id
     * @param status {Boolean} 公司状态
     * @return: Promise
     */
    @Post('updateCompany')
    async updateCompany(@Body() body: updateCompanyDto) {
        try {
            await this.QiyeModel.updateOne({ _id: body.id }, { status: body.status })
            return { code: 200, message: '修改成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }



    /**
     * @Description: 删除公司
     * @param id {String} 公司id
     * @return: Promise
     */
    @Post('deleteCompany')
    async deleteCompany(@Body() body: deleteCompanyDto) {
        try {
            await this.QiyeModel.deleteOne({ _id: body.id })
            return { code: 200, message: '删除成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }



    /**
     * @Description: 删除公司
     * @param id {String} 公司id
     * @return: Promise
     */
    @Post('deleteCompanyAll')
    async deleteCompanyAll() {
        try {
            await this.QiyeModel.deleteMany({})
            return { code: 200, message: '删除成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }



    /**
     * @Description: 查询公司
     * @param pageNo {Number} 页码
     * @param pageSize {Number}  每页条数
     * @return: Promise
     */
    @Get('getCompany')
    async getCompany(@Query() query: getCompanyDto) {
        try {

            let data: { pageNo: number | string, pageSize: number | string } = query
            if (typeof data.pageNo == 'string') {
                data.pageNo = parseInt(data.pageNo)
            }
            if (typeof data.pageSize == 'string') {
                data.pageSize = parseInt(data.pageSize)
            }
            const total = await this.QiyeModel.countDocuments()
            const userlist = await this.QiyeModel.find().skip((data.pageNo - 1) * data.pageSize).limit(data.pageSize).sort({ '_id': -1 });
            return { total, data: userlist }

        } catch (error) {
            throw new BadGatewayException(error)
        }
    }



    /**
   * @Description: 搜索企业
   * @param name {Number} 账户
   * @return: Promise
   */
    @Get('searchCompany')
    async searchCompany(@Query() query: searchCompanyDto) {
        try {
            const userlist = await this.QiyeModel.find({ companyName: query.name })
            return { total: userlist.length, data: userlist }

        } catch (error) {
            throw new BadGatewayException(error)
        }
    }

}
